//
//  PlayViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "PlayViewController.h"
#import "PlayTableViewCell.h"
#import "GetDataMethods.h"
#import "PlayList.h"
#import "PlayDetailsViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "Model.h"
#import "Fit.h"


@interface PlayViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArray;
@property(nonatomic, retain)UIButton *cityButton;
@property(nonatomic, retain)UIButton *chooseButton;
@property(nonatomic, retain)UIView *cityView;
@property(nonatomic, assign)NSInteger number;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation PlayViewController
-(void)dealloc
{
    [_tableView release];
    [_dataArray release];
    [_chooseButton release];
    [_chooseButton release];
    [_cityView release];
    [super dealloc];
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"心晴玩";
    self.dataArray = [NSMutableArray array];
    _number = 0;
    [self reloadDataAndRefresh];
    [self changeColor];
    [self getData];
    
}
-(void)loadView
{
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //加载视图初始化
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
}

-(void)getData
{
    
    NSString *string = @"%E5%85%A8%E9%83%A8%E5%9F%8E%E5%B8%82";
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http://api.breadtrip.com/hunter/products/more/?city_name=%@&sign=8b542b60ef1ca6346eada3b2900c4e3e&start=%ld", string, _number] BodyString:nil WithDataBlock:^(id data) {
        
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [documentPath stringByAppendingPathComponent:@"data2.plist"];
        [data writeToFile:path atomically:YES];

        NSArray *array = [data objectForKey:@"product_list"];
        for (NSDictionary *dic in array) {
            PlayList *play = [[PlayList alloc]init];
            [play setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:play];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_progressView removeFromSuperview];
    }];
}
-(void)reloadDataAndRefresh
{
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        _number = 0;
        [self getData];
        
    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _number += 20;
        [self getData];
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    PlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[PlayTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
        PlayList *play = _dataArray [indexPath.row];
        cell.playList = play;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 400 * H;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayDetailsViewController *pvc = [[PlayDetailsViewController alloc]init];
    if (_dataArray.count != 0) {
        pvc.play = _dataArray[indexPath.row];
    }
    [self.navigationController pushViewController:pvc animated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
